package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    //收货地址模块
    @GetMapping("/address/list")
    public ResultInfo findList() {
        //调用service
        List<Address> addressList = addressService.findList();
        //返回结果
        return ResultInfo.success(addressList);
    }

    //新增收货地址
    @PostMapping("/address")
    public ResultInfo add(@RequestBody Address address) {//接受参数
        //调用service
        addressService.add(address);
        //返回结果
        return ResultInfo.success();

    }

    //回显收货地址
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        //调用service
        Address a = addressService.findById(id);
        //返回结果
        return ResultInfo.success(a);
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo updateAddress(@RequestBody Address address) {
        //service
        addressService.updateAdress(address);
        //返回结果
        return ResultInfo.success();
    }

    //设置默认收货地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String, Long> param) {
        //取出地址id
        Long id = param.get("id");
        //调用service设置默认地址
        addressService.setDefault(id);
        //返回结果
        return ResultInfo.success();


    }

    //查询默认收货地址
    @GetMapping("/address/default")
    public ResultInfo finddefault() {
        //调用service查询默认地址
      Address address=  addressService.findDefault();
        //放回结果
        return ResultInfo.success(address);
    }

}
